import Vue from 'vue';

const base_attr = {
	hp : 10000,
	min_atk : 2,
	max_atk : 5,
	min_def : 2,
	max_def : 5,
	recover : 5,
	crit : 0,
	crit_dmg : 0,
	luck : 0,
	exp : 0,
	add_dmg : 0,
	reduce_dmg: 0,
	speed : 50,
}

export default {
	namespaced: true,

	state: {
		map : 1,
		lv : 1,
		bag_count : 60,
		resource : {
			exp : 0,
			coin : 0
		},
		equip : [],
		bag : [],
		retrieve : [0,0,0,0],
		monster_count : 4,
		skill : [
			{id :1,name:'基本剑术',lv : 1},
		]
	},

	mutations: {
		
		//初始化玩家
		commit_init_player(state, payload){
			updatePlayerAttributes(state)
		},
		commit_map(state,payload){
			state.map = payload 
		},
		commit_resource(state,payload){
			state.resource[payload.type] += payload.value;
			
			if(payload.type == 'exp'){
				getApp().globalData['totalExp'] += payload.value;
				if(state.resource.exp >= getApp().$data_exp[state.lv+1]){
					
					state.resource.exp = 0
					state.lv += 1
					
				}
			}
			
		},
		commit_retrieve_config(state,payload){
			state.retrieve[payload.index] = payload.lv
		},
		commit_monster_count(state,payload){
			state.monster_count = payload
		},
		//背包增加物品
		commit_bag_add(state,itemDataVolume){
			state.bag.push(itemDataVolume)
		},
		//背包删除物品
		commit_bag_remove(state,itemDataVolume){
			state.bag.splice(state.bag.findIndex(item => item.uuid == itemDataVolume.uuid),1);
		},
		commit_put_on_equip(state, equipDataVolume) {
			
			let bodyEquipDataVolume = state.equip.find(item => item.region == equipDataVolume.region);
			let bagEquipDataVolume = equipDataVolume;
		
			if (bodyEquipDataVolume) { // 当前要穿戴的装备类型身上已经有了
				state.equip.splice(state.equip.findIndex(equip => equip.region == equipDataVolume.region),1);
		
				// 获取替换装备在背包中的索引
				let bagIndex = state.bag.findIndex(equip => equip.uuid == bagEquipDataVolume.uuid);
				state.bag.splice(bagIndex, 1);
		
				// 在同样的位置添加新的装备
				state.bag.splice(bagIndex, 0, bodyEquipDataVolume);
		
				state.equip.push(bagEquipDataVolume);
				
			} else { // 当前要穿戴的装备类型身上还没有
				state.bag.splice(state.bag.findIndex(equip => equip.uuid == equipDataVolume.uuid),1);
				state.equip.push(bagEquipDataVolume);
			}
			
			
			updatePlayerAttributes(state)

			
		},
		
		commit_take_off_equip(state, equipDataVolume) {
			
			if(state.bag.length >= state.bag_count){
				return;
			}
			
			
			// 找到要卸下的装备
			let index = state.equip.findIndex(equip => equip.uuid == equipDataVolume.uuid);
			let bodyEquipDataVolume = state.equip.splice(index, 1)[0];
			
			
		
			// 将装备添加到背包
			state.bag.push(bodyEquipDataVolume);
		
			
			updatePlayerAttributes(state)
			
			//$common.save_game()
		},
		
		commit_retrieve(state,equipDataVolumeArr){

			for(let i=0;i<equipDataVolumeArr.length;i++){
				
				if(!equipDataVolumeArr[i].lock){
					state.resource['coin'] += equipDataVolumeArr[i].lv * 10;
					state.bag.splice(state.bag.findIndex(equip => equip.uuid == equipDataVolumeArr[i].uuid),1);
				};

			}

		},
		
		commit_lock(state,equipDataVolume){
			let lock_equip = state.bag.find(item => item.uuid == equipDataVolume.uuid) || state.equip.find(item => item.uuid == equipDataVolume.uuid);
			lock_equip['lock'] = lock_equip.lock ? false : true
		},

	}
}


function updatePlayerAttributes(state) {
	
	state.attr = JSON.parse(JSON.stringify(base_attr));
	
	let player_equips = state.equip;
	
	for(let i=0;i<player_equips.length;i++){
		
		let player_equip_item = player_equips[i];
		
		for (let key in player_equip_item) {
			if(state.attr.hasOwnProperty(key)){
				state.attr[key] += Math.max(0,player_equip_item[key])
			}
		}
		
		if(player_equip_item.special.length){
			for(let i=0;i<player_equip_item.special.length;i++){
				let special = player_equip_item.special[i];
				state.attr[special.name] += Math.max(0,special.value);
			}
		}
		
	}

}
